// CCS file

use "CCS wave 1.dta", replace

// CCS 1 script for CHES

bysort t1 t3: tab a1

// rescale the variables to 0-10 scale
// immigrants should adjust to customs 1 = strongly agree, 5 = strongly disagree
gen multicult_divisions_ccs1 = c2a
replace multicult_divisions_ccs1 = (multicult_divisions_ccs1-1)/(5-1)
replace multicult_divisions_ccs1 = multicult_divisions_ccs1*10

gen imm_eco_divisions_ccs1 = c2j
replace imm_eco_divisions_ccs1 = (imm_eco_divisions_ccs1-1)/(5-1)
replace imm_eco_divisions_ccs1 = imm_eco_divisions_ccs1*10

gen environment_divisions_ccs1 = c2c
replace environment_divisions_ccs1 = (environment_divisions_ccs1-1)/(5-1)
replace environment_divisions_ccs1 = environment_divisions_ccs1*10

gen eu_divisions_ccs1 = c5

// state intervention in the economy is bad, 1 = stronly agree
// transform to left-right scale
gen ecoint_divisions_ccs1 = .
replace ecoint_divisions_ccs1 = 1 if c2b == 5
replace ecoint_divisions_ccs1 = 2 if c2b == 4
replace ecoint_divisions_ccs1 = 3 if c2b == 3
replace ecoint_divisions_ccs1 = 4 if c2b == 2
replace ecoint_divisions_ccs1 = 5 if c2b == 1
replace ecoint_divisions_ccs1 = (ecoint_divisions_ccs1-1)/(5-1)
replace ecoint_divisions_ccs1 = ecoint_divisions_ccs1*10


// redistribution, 1 = strongly agree
gen redist_divisions_ccs1 = c2h if c2h != .
replace redist_divisions_ccs1 = (redist_divisions_ccs1-1)/(5-1)
replace redist_divisions_ccs1 = redist_divisions_ccs1*10

// mix the two economy indicators
// 0 = left, 10 = right
gen economy_divisions_ccs1 = ecoint_divisions_ccs1 + redist_divisions_ccs1 if ecoint_divisions_ccs1 != . & redist_divisions_ccs1 != .
replace economy_divisions_ccs1 = (economy_divisions_ccs1-0)/(20-0)
replace economy_divisions_ccs1 = economy_divisions_ccs1*10

gen lr_divisions_ccs1 = c3

// question about same-sex marriage, 1 = strongly agree prohibition
gen ssm_divisions_ccs1 = .
replace ssm_divisions_ccs1 = 1 if c2d == 5
replace ssm_divisions_ccs1 = 2 if c2d == 4
replace ssm_divisions_ccs1 = 3 if c2d == 3
replace ssm_divisions_ccs1 = 4 if c2d == 2
replace ssm_divisions_ccs1 = 5 if c2d == 1

replace ssm_divisions_ccs1 = (ssm_divisions_ccs1-1)/(5-1)
replace ssm_divisions_ccs1 = ssm_divisions_ccs1*10

// abortion question, 1 = free to decide
gen abortion_divisions_ccs1 = c2k if c2k != .

replace abortion_divisions_ccs1 = (abortion_divisions_ccs1-1)/(5-1)
replace abortion_divisions_ccs1 = abortion_divisions_ccs1*10

// stiffer sentences for law breakers, 1 =  strongly agree
gen order_divisions_ccs1 = .
replace order_divisions_ccs1 = 1 if c2f == 5
replace order_divisions_ccs1 = 2 if c2f == 4
replace order_divisions_ccs1 = 3 if c2f == 3
replace order_divisions_ccs1 = 4 if c2f == 2
replace order_divisions_ccs1 = 5 if c2f == 1

replace order_divisions_ccs1 = (order_divisions_ccs1-1)/(5-1)
replace order_divisions_ccs1 = order_divisions_ccs1*10

// mix the two GALTAN indicators
// 0 = GAL, 10 = TAN
gen galtan_divisions_ccs1 = ssm_divisions_ccs1 + abortion_divisions_ccs1 + order_divisions_ccs1 if ssm_divisions_ccs1 != . & abortion_divisions_ccs1 != . & order_divisions_ccs1 != .
replace galtan_divisions_ccs1 = (galtan_divisions_ccs1-0)/(30-0)
replace galtan_divisions_ccs1 = galtan_divisions_ccs1*10

// CDU Portugal
replace a1 = 4 if a1 == 3 & t1 == 10 & t3 == 2011

// drop all parties with less than 20 observations
gen counter = 1
bysort t1 t3 a1: egen party_observations = count(counter)
keep if party_observations >= 10

collapse (sd) multicult_divisions_ccs1 imm_eco_divisions_ccs1 environment_divisions_ccs1 eu_divisions_ccs1 economy_divisions_ccs1 lr_divisions_ccs1 ssm_divisions_ccs1 abortion_divisions_ccs1 order_divisions_ccs1 galtan_divisions_ccs1 ecoint_divisions_ccs1 redist_divisions_ccs1, by(t1 t3 a1)

save "CCS1_manipulated.dta", replace

use "CCS1_manipulated.dta", replace

gen year = t3

gen ches_year = .
replace ches_year = 2006 if year == 2005
replace ches_year = 2010 if year >= 2006 & year <= 2009
replace ches_year = 2014 if year >= 2010 & year <= 2013
replace ches_year = 2019 if year >= 2014 & year <= 2019

// need to add party ids to the variables
gen party_id = .
gen cmp_id = .
// Germany 2005 + 2009 / be careful when matching with CDU/CSU, generate an average score?
// SPD
replace party_id = 302 if t1 == 3 & a1 == 1 
replace cmp_id = 41320  if t1 == 3 & a1 == 1 
// CDU
replace party_id = 301 if t1 == 3 & a1 == 2 
replace cmp_id = 41521 if t1 == 3 & a1 == 2 
// CSU
replace party_id = 308 if t1 == 3 & a1 == 3 
replace cmp_id = 41521  if t1 == 3 & a1 == 3
// FDP
replace party_id = 303 if t1 == 3 & a1 == 4
replace cmp_id = 41420  if t1 == 3 & a1 == 4 
// Gruene
replace party_id = 304 if t1 == 3 & a1 == 5
replace cmp_id = 41113  if t1 == 3 & a1 == 5
// Left
replace party_id = 306 if t1 == 3 & a1 == 6
replace cmp_id = 41222 if t1 == 3 & a1 == 6 & t3 == 2005
replace cmp_id = 41223 if t1 == 3 & a1 == 6 & t3 == 2009

// Ireland 2007
// FF
replace party_id = 701 if t1 == 4 & a1 == 1 
replace cmp_id = 53620  if t1 == 4 & a1 == 1 
// FG
replace party_id = 702 if t1 == 4 & a1 == 2 
replace cmp_id = 53520  if t1 == 4 & a1 == 2 
// Greens
replace party_id = 705 if t1 == 4 & a1 == 3 
replace cmp_id = 53110 if t1 == 4 & a1 == 3
// Labour
replace party_id = 703 if t1 == 4 & a1 == 4
replace cmp_id = 53320  if t1 == 4 & a1 == 4 
// PD
replace party_id = 706 if t1 == 4 & a1 == 5
replace cmp_id = 53420  if t1 == 4 & a1 == 5
// SF
replace party_id = 707 if t1 == 4 & a1 == 6
replace cmp_id = 53951  if t1 == 4 & a1 == 6

// Greece 2007+2009
// PASOK
replace party_id = 402 if t1 == 5 & a1 == 1 
replace cmp_id = 34511  if t1 == 5 & a1 == 1 
// ND
replace party_id = 401 if t1 == 5 & a1 == 2 
replace cmp_id = 34313  if t1 == 5 & a1 == 2 

// Greece 2012
// Syriza, careful when matching as it also has cmp 34020 for the earlier 2012 elections
replace party_id = 403 if t1 == 5 & a1 == 3 
replace cmp_id = 34212  if t1 == 5 & a1 == 3
// ANEL
replace party_id =  412 if t1 == 5 & a1 == 4
replace cmp_id = 34730  if t1 == 5 & a1 == 4 
// XA
replace party_id = 415 if t1 == 5 & a1 == 5
replace cmp_id = 34720   if t1 == 5 & a1 == 5
// DIMAR
replace party_id = 414 if t1 == 5 & a1 == 6
replace cmp_id = 34213  if t1 == 5 & a1 == 6


// Finland 2007
// Centre
replace party_id = 1403 if t1 == 6 & a1 == 1 & t3 == 2007
replace cmp_id = 14810  if t1 == 6 & a1 == 1 & t3 == 2007
// National Coalition
replace party_id = 1402 if t1 == 6 & a1 == 2 & t3 == 2007
replace cmp_id = 14620  if t1 == 6 & a1 == 2 & t3 == 2007
// Social Dems
replace party_id = 1401 if t1 == 6 & a1 == 3 & t3 == 2007
replace cmp_id = 14320  if t1 == 6 & a1 == 3 & t3 == 2007
// Left Alliance
replace party_id = 1404 if t1 == 6 & a1 == 4 & t3 == 2007
replace cmp_id = 14223  if t1 == 6 & a1 == 4  & t3 == 2007
// Greens
replace party_id = 1408 if t1 == 6 & a1 == 5 & t3 == 2007
replace cmp_id = 14110 if t1 == 6 & a1 == 5 & t3 == 2007
// KD
replace party_id = 1409 if t1 == 6 & a1 == 6 & t3 == 2007
replace cmp_id = 14520 if t1 == 6 & a1 == 6 & t3 == 2007
// Swedish
replace party_id = 1406 if t1 == 6 & a1 == 7 & t3 == 2007
replace cmp_id = 14901  if t1 == 6 & a1 == 7 & t3 == 2007
// PS
replace party_id = 1405 if t1 == 6 & a1 == 8 & t3 == 2007
replace cmp_id = 14820  if t1 == 6 & a1 == 8 & t3 == 2007

// Finland 2011
// Centre
replace party_id = 1403 if t1 == 6 & a1 == 4 & t3 == 2011
replace cmp_id = 14810  if t1 == 6 & a1 == 4 & t3 == 2011
// National Coalition
replace party_id = 1402 if t1 == 6 & a1 == 1 & t3 == 2011
replace cmp_id = 14620  if t1 == 6 & a1 == 1 & t3 == 2011
// Social Dems
replace party_id = 1401 if t1 == 6 & a1 == 2 & t3 == 2011
replace cmp_id = 14320  if t1 == 6 & a1 == 2 & t3 == 2011
// Left Alliance
replace party_id = 1404 if t1 == 6 & a1 == 5 & t3 == 2011
replace cmp_id = 14223  if t1 == 6 & a1 == 5  & t3 == 2011
// Greens
replace party_id = 1408 if t1 == 6 & a1 == 6 & t3 == 2011
replace cmp_id = 14110 if t1 == 6 & a1 == 6 & t3 == 2011
// KD
replace party_id = 1409 if t1 == 6 & a1 == 7 & t3 == 2011
replace cmp_id = 14520 if t1 == 6 & a1 == 7 & t3 == 2011
// Swedish
replace party_id = 1406 if t1 == 6 & a1 == 8 & t3 == 2011
replace cmp_id = 14901  if t1 == 6 & a1 == 8 & t3 == 2011
// PS
replace party_id = 1405 if t1 == 6 & a1 == 3 & t3 == 2011
replace cmp_id = 14820  if t1 == 6 & a1 == 3 & t3 == 2011

// Belgium 2007
// PS
replace party_id = 102 if t1 == 7 & a1 == 1 & t3 == 2007
replace cmp_id = 21322  if t1 == 7 & a1 == 1 & t3 == 2007
// MR
replace party_id = 106 if t1 == 7 & a1 == 2 & t3 == 2007
replace cmp_id = 21426  if t1 == 7 & a1 == 2 & t3 == 2007
// CDH
replace party_id = 108 if t1 == 7 & a1 == 3 & t3 == 2007
replace cmp_id = 21522  if t1 == 7 & a1 == 3 & t3 == 2007
// Ecolo
replace party_id = 104 if t1 == 7 & a1 == 4 & t3 == 2007
replace cmp_id = 21111  if t1 == 7 & a1 == 4  & t3 == 2007
// FN
replace party_id = 115 if t1 == 7 & a1 == 5 & t3 == 2007
replace cmp_id = . if t1 == 7 & a1 == 5 & t3 == 2007
// CDV
replace party_id = 109 if t1 == 7 & a1 == 11 & t3 == 2007
replace cmp_id = 21521  if t1 == 7 & a1 == 11 & t3 == 2007
// NVA
replace party_id = 110 if t1 == 7 & a1 == 12 & t3 == 2007
replace cmp_id = 21916  if t1 == 7 & a1 == 12 & t3 == 2007
// SPA
replace party_id = 103 if t1 == 7 & a1 == 13 & t3 == 2007
replace cmp_id = 21321 if t1 == 7 & a1 == 13 & t3 == 2007
// Open VLD
replace party_id = 107 if t1 == 7 & a1 == 15 & t3 == 2007
replace cmp_id = 21421  if t1 == 7 & a1 == 15 & t3 == 2007
// VB
replace party_id = 112 if t1 == 7 & a1 == 16 & t3 == 2007
replace cmp_id = 21917 if t1 == 7 & a1 == 16 & t3 == 2007
// Groen!
replace party_id = 105 if t1 == 7 & a1 == 17 & t3 == 2007
replace cmp_id = 21112  if t1 == 7 & a1 == 17 & t3 == 2007

// Belgium 2010
// PS
replace party_id = 102 if t1 == 7 & a1 == 1 & t3 == 2010
replace cmp_id = 21322  if t1 == 7 & a1 == 1 & t3 == 2010
// MR
replace party_id = 106 if t1 == 7 & a1 == 2 & t3 == 2010
replace cmp_id = 21426  if t1 == 7 & a1 == 2 & t3 == 2010
// CDH
replace party_id = 108 if t1 == 7 & a1 == 3 & t3 == 2010
replace cmp_id = 21522  if t1 == 7 & a1 == 3 & t3 == 2010
// Ecolo
replace party_id = 104 if t1 == 7 & a1 == 4 & t3 == 2010
replace cmp_id = 21111  if t1 == 7 & a1 == 4  & t3 == 2010
// FDF
replace party_id = 111 if t1 == 7 & a1 == 6 & t3 == 2010
replace cmp_id = 21423   if t1 == 7 & a1 == 6  & t3 == 2010
// VU
replace party_id = 110 if t1 == 7 & a1 == 8 & t3 == 2010
replace cmp_id = 21913   if t1 == 7 & a1 == 8  & t3 == 2010

// CDV
replace party_id = 109 if t1 == 7 & a1 == 11 & t3 == 2010
replace cmp_id = 21521  if t1 == 7 & a1 == 11 & t3 == 2010
// NVA
replace party_id = 110 if t1 == 7 & a1 == 12 & t3 == 2010
replace cmp_id = 21916  if t1 == 7 & a1 == 12 & t3 == 2010
// SPA
replace party_id = 103 if t1 == 7 & a1 == 13 & t3 == 2010
replace cmp_id = 21321 if t1 == 7 & a1 == 13 & t3 == 2010
// Open VLD
replace party_id = 107 if t1 == 7 & a1 == 15 & t3 == 2010
replace cmp_id = 21421  if t1 == 7 & a1 == 15 & t3 == 2010
// VB
replace party_id = 112 if t1 == 7 & a1 == 16 & t3 == 2010
replace cmp_id = 21917 if t1 == 7 & a1 == 16 & t3 == 2010
// Groen!
replace party_id = 105 if t1 == 7 & a1 == 17 & t3 == 2010
replace cmp_id = 21112  if t1 == 7 & a1 == 17 & t3 == 2010

// Netherlands 2006
// CDA
replace party_id = 1001 if t1 == 8 & a1 == 1 
replace cmp_id = 22521  if t1 == 8 & a1 == 1 
// PvdA
replace party_id = 1002 if t1 == 8 & a1 == 2 
replace cmp_id = 22320 if t1 == 8 & a1 == 2 
// SP
replace party_id = 1014 if t1 == 8 & a1 == 3 
replace cmp_id = 22220  if t1 == 8 & a1 == 3
// VVD
replace party_id = 1003 if t1 == 8 & a1 == 4
replace cmp_id = 22420  if t1 == 8 & a1 == 4 
// PVV
replace party_id = 1017 if t1 == 8 & a1 == 5
replace cmp_id = 22722 if t1 == 8 & a1 == 5
// GL
replace party_id = 1005 if t1 == 8 & a1 == 6
replace cmp_id = 22110  if t1 == 8 & a1 == 6
// CU
replace party_id = 1016 if t1 == 8 & a1 == 7
replace cmp_id = 22526  if t1 == 8 & a1 == 7
// D66
replace party_id = 1004 if t1 == 8 & a1 == 8
replace cmp_id = 22330  if t1 == 8 & a1 == 8
// SGP
replace party_id = 1006 if t1 == 8 & a1 == 9
replace cmp_id = 22952  if t1 == 8 & a1 == 9

// Portugal 2009
// BE
replace party_id = 1208 if t1 == 10 & a1 == 1 & t3 == 2009
replace cmp_id = 35211  if t1 == 10 & a1 == 1 & t3 == 2009
// CDS-PP
replace party_id = 1202 if t1 == 10 & a1 == 2 & t3 == 2009
replace cmp_id = 35520  if t1 == 10 & a1 == 2 & t3 == 2009
// CDU
replace party_id = 1201 if t1 == 10 & a1 == 3 & t3 == 2009
replace cmp_id = 35229  if t1 == 10 & a1 == 3 & t3 == 2009
// PSD 
replace party_id =  1206 if t1 == 10 & a1 == 4 & t3 == 2009
replace cmp_id = 35313  if t1 == 10 & a1 == 4  & t3 == 2009
// PS
replace party_id = 1205 if t1 == 10 & a1 == 5 & t3 == 2009
replace cmp_id = 35311 if t1 == 10 & a1 == 5 & t3 == 2009

// Portugal 2011
// BE
replace party_id = 1208 if t1 == 10 & a1 == 1 & t3 == 2011
replace cmp_id = 35211  if t1 == 10 & a1 == 1 & t3 == 2011
// CDS-PP
replace party_id = 1202 if t1 == 10 & a1 == 2 & t3 == 2011
replace cmp_id = 35520  if t1 == 10 & a1 == 2 & t3 == 2011
// PEV
replace party_id = 1201 if t1 == 10 & a1 == 3 & t3 == 2011
replace cmp_id = 35110   if t1 == 10 & a1 == 3 & t3 == 2011
// PCP
replace party_id = 1201 if t1 == 10 & a1 == 4 & t3 == 2011
replace cmp_id = 35220   if t1 == 10 & a1 == 4  & t3 == 2011
// PSD
replace party_id = 1206 if t1 == 10 & a1 == 5 & t3 == 2011
replace cmp_id = 35313   if t1 == 10 & a1 == 5 & t3 == 2011
// PS
replace party_id = 1205 if t1 == 10 & a1 == 6 & t3 == 2011
replace cmp_id = 35311 if t1 == 10 & a1 == 6 & t3 == 2011

// Austria 2008
// SPÖ
replace party_id = 1301 if t1 == 14 & a1 == 1 
replace cmp_id = 42320  if t1 == 14 & a1 == 1 
// ÖVP
replace party_id = 1302 if t1 == 14 & a1 == 2 
replace cmp_id = 42520  if t1 == 14 & a1 == 2 
// Gruene
replace party_id = 1304 if t1 == 14 & a1 == 3 
replace cmp_id = 42110  if t1 == 14 & a1 == 3
// FPÖ
replace party_id = 1303 if t1 == 14 & a1 == 4
replace cmp_id = 42420 if t1 == 14 & a1 == 4 
// BZÖ
replace party_id = 1307 if t1 == 14 & a1 == 5
replace cmp_id = 42710  if t1 == 14 & a1 == 5
// KPÖ
replace party_id = . if t1 == 14 & a1 == 8
replace cmp_id = 42220  if t1 == 14 & a1 == 8
// LIF
replace party_id = 1306 if t1 == 14 & a1 == 9
replace cmp_id = 42421   if t1 == 14 & a1 == 9

// Sweden 2010
// C
replace party_id = 1603 if t1 == 15 & a1 == 1 
replace cmp_id = 11810  if t1 == 15 & a1 == 1 
// FI
replace party_id = 1612 if t1 == 15 & a1 == 2 
replace cmp_id = . if t1 == 15 & a1 == 2 
// L
replace party_id = 1604 if t1 == 15 & a1 == 3 
replace cmp_id = 11420  if t1 == 15 & a1 == 3
// KD
replace party_id = 1606 if t1 == 15 & a1 == 4
replace cmp_id = 11520  if t1 == 15 & a1 == 4 
// MP
replace party_id = 1607 if t1 == 15 & a1 == 5
replace cmp_id = 11110 if t1 == 15 & a1 == 5
// M
replace party_id = 1605 if t1 == 15 & a1 == 6
replace cmp_id = 11620  if t1 == 15 & a1 == 6
// S
replace party_id = 1602  if t1 == 15 & a1 == 7
replace cmp_id = 11320  if t1 == 15 & a1 == 7
// SD
replace party_id = 1610 if t1 == 15 & a1 == 8
replace cmp_id = 11710   if t1 == 15 & a1 == 8
// V
replace party_id = 1601 if t1 == 15 & a1 == 9
replace cmp_id = 11220 if t1 == 15 & a1 == 9


// Denmark 2011
// Red-Green
replace party_id = 213 if t1 == 16 & a1 == 1 
replace cmp_id = 13229 if t1 == 16 & a1 == 1 
// SF
replace party_id = 206 if t1 == 16 & a1 == 2 
replace cmp_id = 13230  if t1 == 16 & a1 == 2 
// SD
replace party_id = 201 if t1 == 16 & a1 == 3 
replace cmp_id = 13320  if t1 == 16 & a1 == 3
// RV
replace party_id = 202 if t1 == 16 & a1 == 4
replace cmp_id = 13410  if t1 == 16 & a1 == 4 
// KD
replace party_id = 210 if t1 == 16 & a1 == 5
replace cmp_id = 13520  if t1 == 16 & a1 == 5
// LA (NA)
replace party_id = 218 if t1 == 16 & a1 == 6
replace cmp_id = 13001  if t1 == 16 & a1 == 6
// V
replace party_id = 211 if t1 == 16 & a1 == 7
replace cmp_id = 13420  if t1 == 16 & a1 == 7
// KF
replace party_id = 203 if t1 == 16 & a1 == 8
replace cmp_id = 13620  if t1 == 16 & a1 == 8
// DF
replace party_id = 215 if t1 == 16 & a1 == 9
replace cmp_id = 13720  if t1 == 16 & a1 == 9
// Center Democrats (CD)
replace party_id = 204 if t1 == 16 & a1 == 15
replace cmp_id = 13330  if t1 == 16 & a1 == 15

// Italy 2013
	// CENTRO DEMOCRATICO
	// replace party_id =  if t1 == 21 & a1 == 1 
	// replace cmp_id =  if t1 == 21 & a1 == 1 
// FRATELLI D'ITALIA
replace party_id = 844 if t1 == 21 & a1 == 2 
replace cmp_id = 32630 if t1 == 21 & a1 == 2 
// IL POPOLO DELLA LIBERTA'
replace party_id = 815            if t1 == 21 & a1 == 3 
replace cmp_id = 32610             if t1 == 21 & a1 == 3
// LEGA NORD
replace party_id = 811            if t1 == 21 & a1 == 4
replace cmp_id = 32720            if t1 == 21 & a1 == 4 
// MOVIMENTO 5 STELLE BEPPEGRILLO
replace party_id =  845           if t1 == 21 & a1 == 5
replace cmp_id = 32956     if t1 == 21 & a1 == 5
// PARTITO DEMOCRATICO
replace party_id = 837             if t1 == 21 & a1 == 6
replace cmp_id = 32440             if t1 == 21 & a1 == 6
*	// RIVOLUZIONE CIVILE
replace party_id = 803 if t1 == 21 & a1 == 7
*	replace cmp_id =  if t1 == 21 & a1 == 7
*	// SCELTA CIVICA CON MONTI PER L'ITALIA
*	replace party_id =  if t1 == 21 & a1 == 8
*	replace cmp_id =  if t1 == 21 & a1 == 8
*	// SINISTRA ECOLOGIA LIBERTA
*	replace party_id =  if t1 == 21 & a1 == 9
*	replace cmp_id =  if t1 == 21 & a1 == 9
*	// SVP
replace party_id = 827 if t1 == 21 & a1 == 10
*	replace cmp_id =  if t1 == 21 & a1 == 10
*	// UNIONE DI CENTRO.
replace party_id =  814 if t1 == 21 & a1 == 11
replace cmp_id = 32530 if t1 == 21 & a1 == 11
*
// UK 2010
// Labor
replace party_id = 1102 if t1 == 23 & a1 == 1 
replace cmp_id = 51320  if t1 == 23 & a1 == 1 
// Cons
replace party_id = 1101 if t1 == 23 & a1 == 2 
replace cmp_id = 51620  if t1 == 23 & a1 == 2 
// LibDems
replace party_id = 1104 if t1 == 23 & a1 == 3 
replace cmp_id = 51421  if t1 == 23 & a1 == 3
// UKIP
replace party_id = 1108 if t1 == 23 & a1 == 4
replace cmp_id = 51951  if t1 == 23 & a1 == 4 
// GPEW
replace party_id = 1107 if t1 == 23 & a1 == 5
replace cmp_id = 51110 if t1 == 23 & a1 == 5
// SNP
replace party_id = 1105 if t1 == 23 & a1 == 6
replace cmp_id = 51902  if t1 == 23 & a1 == 6
// Plaid
replace party_id = 1106 if t1 == 23 & a1 == 7
replace cmp_id = 51901  if t1 == 23 & a1 == 7

// Estonia
// Estonian Independence Party
replace party_id = . if t1 == 11 & a1 == 1
replace cmp_id =  . if t1 == 11 & a1 == 1
// Estonian Center Party
replace party_id = 2202 if t1 == 11 & a1 == 2
replace cmp_id = 83411  if t1 == 11 & a1 == 2
// Estonian Reform Party
replace party_id = 2203 if t1 == 11 & a1 == 3
replace cmp_id = 83430  if t1 == 11 & a1 == 3
// People's Union of Estonia
replace party_id = 2206 if t1 == 11 & a1 == 4
replace cmp_id = 83612  if t1 == 11 & a1 == 4
// Estonian Christian Democrats
replace party_id = . if t1 == 11 & a1 == 5
replace cmp_id = .  if t1 == 11 & a1 == 5
// Estonian Greens
replace party_id = 2207 if t1 == 11 & a1 == 6
replace cmp_id = 83110  if t1 == 11 & a1 == 6
// Union of Pro Patria and Res Publica
replace party_id = 2201 if t1 == 11 & a1 == 7
replace cmp_id = 83611  if t1 == 11 & a1 == 7
// Social Democratic Party
replace party_id = 2204 if t1 == 11 & a1 == 8
replace cmp_id = 83410  if t1 == 11 & a1 == 8
// Russian Party in Estonia
replace party_id = . if t1 == 11 & a1 == 9
replace cmp_id = .  if t1 == 11 & a1 == 9


// Hungary
// Fidesz
replace party_id = 2302 if t1 == 13 & a1 == 1
replace cmp_id =  86421 if t1 == 13 & a1 == 1
// KDNP
replace party_id = 2307 if t1 == 13 & a1 == 2
replace cmp_id = 86522  if t1 == 13 & a1 == 2
// MSZP
replace party_id = 2301 if t1 == 13 & a1 == 3
replace cmp_id = 86220  if t1 == 13 & a1 == 3
// Jobbik
replace party_id = 2308 if t1 == 13 & a1 == 4
replace cmp_id = 86710 if t1 == 13 & a1 == 4
// LMP
replace party_id = 2309 if t1 == 13 & a1 == 5
replace cmp_id = 86110 if t1 == 13 & a1 == 5


// Romania
// PSD
replace party_id = 2701 if t1 == 17 & a1 == 1
replace cmp_id =  93223 if t1 == 17 & a1 == 1
// PNL
replace party_id = 2705 if t1 == 17 & a1 == 2
replace cmp_id = 93430  if t1 == 17 & a1 == 2
// PC
replace party_id = 2702 if t1 == 17 & a1 == 3
replace cmp_id = .  if t1 == 17 & a1 == 3
// UNPR
replace party_id = 2709 if t1 == 17 & a1 == 4
replace cmp_id = .  if t1 == 17 & a1 == 4
// USL
replace party_id = . if t1 == 17 & a1 == 5
replace cmp_id = .  if t1 == 17 & a1 == 5
// PDL
replace party_id = 2704 if t1 == 17 & a1 == 6
replace cmp_id = 93530  if t1 == 17 & a1 == 6
// PN?CD
replace party_id = . if t1 == 17 & a1 == 7
replace cmp_id = .  if t1 == 17 & a1 == 7
// FC
replace party_id = . if t1 == 17 & a1 == 8
replace cmp_id = .  if t1 == 17 & a1 == 8
// ARD
replace party_id = . if t1 == 17 & a1 == 9
replace cmp_id = .  if t1 == 17 & a1 == 9
// UDMR
replace party_id = 2706 if t1 == 17 & a1 == 10
replace cmp_id = 93951  if t1 == 17 & a1 == 10
// PPDD
replace party_id = 2710 if t1 == 17 & a1 == 11
replace cmp_id = 93981  if t1 == 17 & a1 == 11



// Czech Republic
// CSSD
replace party_id = 2101 if t1 == 18 & a1 == 1
replace cmp_id =  82320 if t1 == 18 & a1 == 1
// KDU
replace party_id = 2104 if t1 == 18 & a1 == 2
replace cmp_id = 82523  if t1 == 18 & a1 == 2
// KSCM
replace party_id = 2103 if t1 == 18 & a1 == 3
replace cmp_id = 82220  if t1 == 18 & a1 == 3
// ODS
replace party_id = 2102 if t1 == 18 & a1 == 4
replace cmp_id = 82413  if t1 == 18 & a1 == 4
// SZ
replace party_id = 2107 if t1 == 18 & a1 == 5
replace cmp_id = 82110  if t1 == 18 & a1 == 5



// Malta
// Partit Nazzjonalista
replace party_id = 3702 if t1 == 22 & a1 == 1
replace cmp_id = 54620 if t1 == 22 & a1 == 1
// Partit Laburista
replace party_id = 3701 if t1 == 22 & a1 == 2
replace cmp_id = 54320  if t1 == 22 & a1 == 2
// Alternattiva Demokratika
replace party_id = . if t1 == 22 & a1 == 3
replace cmp_id = .  if t1 == 22 & a1 == 3


// Luxembourg
// Parti Communiste Luxembourgeois (PCL-KPL)
replace party_id = . if t1 == 24 & a1 == 1
replace cmp_id =  23220 if t1 == 24 & a1 == 1
// La Gauche
replace party_id = 3806 if t1 == 24 & a1 == 2
replace cmp_id = 23230 if t1 == 24 & a1 == 2
// Les Verts
replace party_id = 3802 if t1 == 24 & a1 == 3
replace cmp_id = 23113 if t1 == 24 & a1 == 3
// Parti Ouvrier Socialiste Luxembourgeois (POSL-LSAP)
replace party_id = 3804 if t1 == 24 & a1 == 4
replace cmp_id = 23320  if t1 == 24 & a1 == 4
// Parti Démocratique (PD-DP)
replace party_id = 3803 if t1 == 24 & a1 == 5
replace cmp_id = 23420  if t1 == 24 & a1 == 5
// Parti Chrétien Social (PCS_CSV)
replace party_id = 3801 if t1 == 24 & a1 == 6
replace cmp_id = 23520  if t1 == 24 & a1 == 6
// Parti Démocrate Réformateur (ADR)
replace party_id = 3805 if t1 == 24 & a1 == 7
replace cmp_id = 23951  if t1 == 24 & a1 == 7
// Bierger Lëscht (BL)
replace party_id = . if t1 == 24 & a1 == 8
replace cmp_id = .  if t1 == 24 & a1 == 8


//drop if cmp_id == . 
// drop CSU
//drop if party_id == 308


drop if party_id == . 

// drop Greece 2007
drop if t1 == 5 & year == 2007


save "CCS_1_manipulated.dta", replace

// wave 2

use "CCS wave 2.dta", replace


// rescale the variables to 0-10 scale
// immigrants should adjust to customs 1 = strongly agree, 5 = strongly disagree
gen multicult_divisions_ccs2 = C2a
replace multicult_divisions_ccs2 = (multicult_divisions_ccs2-1)/(5-1)
replace multicult_divisions_ccs2 = multicult_divisions_ccs2*10

// 1 = immigrants good for the economy, 5 = bad
gen imm_eco_divisions_ccs2 = C2i
replace imm_eco_divisions_ccs2 = (imm_eco_divisions_ccs2-1)/(5-1) if T1 != 23
replace imm_eco_divisions_ccs2 = imm_eco_divisions_ccs2*10 if T1 != 23
replace imm_eco_divisions_ccs2 = (imm_eco_divisions_ccs2-1)/(7-1) if T1 == 23
replace imm_eco_divisions_ccs2 = imm_eco_divisions_ccs2*10 if T1 == 23

gen environment_divisions_ccs2 = C2c
replace environment_divisions_ccs2 = (environment_divisions_ccs2-1)/(5-1)
replace environment_divisions_ccs2 = environment_divisions_ccs2*10

gen eu_divisions_ccs2 = C5


gen lr_divisions_ccs2 = C3a


// state intervention in the economy is bad, 1 = stronly agree
// transform to left-right scale
gen ecoint_divisions_ccs2 = .
replace ecoint_divisions_ccs2 = 1 if C2b == 5
replace ecoint_divisions_ccs2 = 2 if C2b == 4
replace ecoint_divisions_ccs2 = 3 if C2b == 3
replace ecoint_divisions_ccs2 = 4 if C2b == 2
replace ecoint_divisions_ccs2 = 5 if C2b == 1
replace ecoint_divisions_ccs2 = (ecoint_divisions_ccs2-1)/(5-1)
replace ecoint_divisions_ccs2 = ecoint_divisions_ccs2*10

// redistribution, 1 = strongly agree
gen redist_divisions_ccs2 = C2h if C2h != .
replace redist_divisions_ccs2 = (redist_divisions_ccs2-1)/(5-1)
replace redist_divisions_ccs2 = redist_divisions_ccs2*10

// mix the two economy indicators
// 0 = left, 10 = right
gen economy_divisions_ccs2 = ecoint_divisions_ccs2 + redist_divisions_ccs2 if ecoint_divisions_ccs2 != . & redist_divisions_ccs2 != .
replace economy_divisions_ccs2 = (economy_divisions_ccs2-0)/(20-0)
replace economy_divisions_ccs2 = economy_divisions_ccs2*10

// question about same-sex marriage, 1 = strongly agree prohibition
gen ssm_divisions_ccs2 = .
replace ssm_divisions_ccs2 = 1 if C2d == 5
replace ssm_divisions_ccs2 = 2 if C2d == 4
replace ssm_divisions_ccs2 = 3 if C2d == 3
replace ssm_divisions_ccs2 = 4 if C2d == 2
replace ssm_divisions_ccs2 = 5 if C2d == 1

replace ssm_divisions_ccs2 = (ssm_divisions_ccs2-1)/(5-1)
replace ssm_divisions_ccs2 = ssm_divisions_ccs2*10

// abortion question, 1 = free to decide
gen abortion_divisions_ccs2 = C2j if C2j != .

replace abortion_divisions_ccs2 = (abortion_divisions_ccs2-1)/(5-1)
replace abortion_divisions_ccs2 = abortion_divisions_ccs2*10

// stiffer sentences for law breakers, 1 =  strongly agree
gen order_divisions_ccs2 = .
replace order_divisions_ccs2 = 1 if C2f == 5
replace order_divisions_ccs2 = 2 if C2f == 4
replace order_divisions_ccs2 = 3 if C2f == 3
replace order_divisions_ccs2 = 4 if C2f == 2
replace order_divisions_ccs2 = 5 if C2f == 1

replace order_divisions_ccs2 = (order_divisions_ccs2-1)/(5-1)
replace order_divisions_ccs2 = order_divisions_ccs2*10

// mix the two GALTAN indicators
// 0 = GAL, 10 = TAN
gen galtan_divisions_ccs2 = ssm_divisions_ccs2 + abortion_divisions_ccs2 + order_divisions_ccs2 if ssm_divisions_ccs2 != . & abortion_divisions_ccs2 != . & order_divisions_ccs2 != .
replace galtan_divisions_ccs2 = (galtan_divisions_ccs2-0)/(30-0)
replace galtan_divisions_ccs2 = galtan_divisions_ccs2*10

// drop all parties with less than 20 observations
gen counter = 1
bysort T1 T3 A1: egen party_observations = count(counter)
keep if party_observations >= 10


collapse (sd) multicult_divisions_ccs2 imm_eco_divisions_ccs2 environment_divisions_ccs2 eu_divisions_ccs2 economy_divisions_ccs2 lr_divisions_ccs2 ssm_divisions_ccs2 abortion_divisions_ccs2 order_divisions_ccs2 galtan_divisions_ccs2 ecoint_divisions_ccs2 redist_divisions_ccs2, by(T1 T3 A1)

save "CCS2_manipulated.dta", replace

use "CCS2_manipulated.dta", replace

gen year = T3 

gen ches_year = .
replace ches_year = 2006 if year == 2005
replace ches_year = 2010 if year >= 2006 & year <= 2009
replace ches_year = 2014 if year >= 2010 & year <= 2013
replace ches_year = 2019 if year >= 2014 & year <= 2019


// need to add party ids to the variables
gen party_id = .
gen cmp_id = .
// Germany 2013 / be careful when matching with CDU/CSU, generate an average score?
// SPD
replace party_id = 302 if T1 == 1 & A1 == 4 
replace cmp_id = 41320  if T1 == 1 & A1 == 4 
// CDU
replace party_id = 301 if T1 == 1 & A1 == 2
replace cmp_id = 41521 if T1 == 1 & A1 == 2 
// CSU
replace party_id = 308 if T1 == 1 & A1 == 3 
replace cmp_id = 41521  if T1 == 1 & A1 == 3
// FDP
replace party_id = 303 if T1 == 1 & A1 == 7 & T3 == 2013
replace cmp_id = 41420  if T1 == 1 & A1 == 7 & T3 == 2013
// Gruene
replace party_id = 304 if T1 == 1 & A1 == 6 & T3 == 2013
replace cmp_id = 41113  if T1 == 1 & A1 == 6 & T3 == 2013
// Left 
replace party_id = 306 if T1 == 1 & A1 == 5 & T3 == 2013
replace cmp_id = 41223 if T1 == 1 & A1 == 5 & T3 == 2013
// AfD
replace party_id = 310 if T1 == 1 & A1 == 8 & T3 == 2013
replace cmp_id = 41953  if T1 == 1 & A1 == 8 & T3 == 2013
// Pirates
replace party_id = 311 if T1 == 1 & A1 == 9 & T3 == 2013
replace cmp_id = 41952  if T1 == 1 & A1 == 9 & T3 == 2013

// Germany 2017
// SPD
replace party_id = 302 if T1 == 22 & A1 == 4 
replace cmp_id = 41320  if T1 == 22 & A1 == 4 
// CDU
replace party_id = 301 if T1 == 22 & A1 == 2
replace cmp_id = 41521 if T1 == 22 & A1 == 2 
// CSU
replace party_id = 308 if T1 == 22 & A1 == 3 
replace cmp_id = 41521  if T1 == 22 & A1 == 3
// FDP
replace party_id = 303 if T1 == 22 & A1 == 6 & T3 == 2017
replace cmp_id = 41420  if T1 == 22 & A1 == 6 & T3 == 2017
// Gruene
replace party_id = 304 if T1 == 22 & A1 == 8 & T3 == 2017
replace cmp_id = 41113  if T1 == 22 & A1 == 8 & T3 == 2017
// Left
replace party_id = 306 if T1 == 22 & A1 == 7 & T3 == 2017
replace cmp_id = 41223 if T1 == 22 & A1 == 7 & T3 == 2017
// AfD
replace party_id = 310 if T1 == 22 & A1 == 5 & T3 == 2017
replace cmp_id = 41953  if T1 == 22 & A1 == 5 & T3 == 2017


// Greece 2015
// PASOK
replace party_id = 402 if T1 == 4 & A1 == 7
replace cmp_id = 34511  if T1 == 4 & A1 == 7
// ND
replace party_id = 401 if T1 == 4 & A1 == 2
replace cmp_id = 34313  if T1 == 4 & A1 == 2 
// Syriza, careful when matching as it also has cmp 34020 for the earlier 2012 elections
replace party_id = 403 if T1 == 4 & A1 == 1
replace cmp_id = 34212  if T1 == 4 & A1 == 1
// ANEL
replace party_id =  412 if T1 == 4 & A1 == 6
replace cmp_id = 34730  if T1 == 4 & A1 == 6
// XA
replace party_id = 415 if T1 == 4 & A1 == 3
replace cmp_id = 34720   if T1 == 4 & A1 == 3
// River
replace party_id =  413 if T1 == 4 & A1 == 4
replace cmp_id = 34340   if T1 == 4 & A1 == 4
// KKE
replace party_id = 404 if T1 == 4 & A1 == 5
replace cmp_id =  34210 if T1 == 4 & A1 == 5

// Sweden 2014
// C
replace party_id = 1603 if T1 == 6 & A1 == 1 
replace cmp_id = 11810  if T1 == 6 & A1 == 1 
// FI
replace party_id = 1612 if T1 == 6 & A1 == 2 
replace cmp_id = . if T1 == 6 & A1 == 2 
// L
replace party_id = 1604 if T1 == 6 & A1 == 3 
replace cmp_id = 11420  if T1 == 6 & A1 == 3
// KD
replace party_id = 1606 if T1 == 6 & A1 == 4
replace cmp_id = 11520  if T1 == 6 & A1 == 4 
// MP
replace party_id = 1607 if T1 == 6 & A1 == 5
replace cmp_id = 11110 if T1 == 6 & A1 == 5
// M
replace party_id = 1605 if T1 == 6 & A1 == 6
replace cmp_id = 11620  if T1 == 6 & A1 == 6
// S
replace party_id = 1602  if T1 == 6 & A1 == 7
replace cmp_id = 11320  if T1 == 6 & A1 == 7
// SD
replace party_id = 1610 if T1 == 6 & A1 == 8
replace cmp_id = 11710   if T1 == 6 & A1 == 8
// V
replace party_id = 1601 if T1 == 6 & A1 == 9
replace cmp_id = 11220 if T1 == 6 & A1 == 9

// Finland 2015

// Centre
replace party_id = 1403 if T1 == 15 & A1 == 4
replace cmp_id = 14810  if T1 == 15 & A1 == 4
// National Coalition
replace party_id = 1402 if T1 == 15 & A1 == 1 
replace cmp_id = 14620  if T1 == 15 & A1 == 1
// Social Dems
replace party_id = 1401 if T1 == 15 & A1 == 2 
replace cmp_id = 14320  if T1 == 15 & A1 == 2 
// Left Alliance
replace party_id = 1404 if T1 == 15 & A1 == 5 
replace cmp_id = 14223  if T1 == 15 & A1 == 5  
// Greens
replace party_id = 1408 if T1 == 15 & A1 == 6 
replace cmp_id = 14110 if T1 == 15 & A1 == 6 
// KD
replace party_id = 1409 if T1 == 15 & A1 == 7 
replace cmp_id = 14520 if T1 == 15 & A1 == 7 
// Swedish
replace party_id = 1406 if T1 == 15 & A1 == 8
replace cmp_id = 14901  if T1 == 15 & A1 == 8 
// PS
replace party_id = 1405 if T1 == 15 & A1 == 3 
replace cmp_id = 14820  if T1 == 15 & A1 == 3 

// Portugal 2015
// BE
replace party_id = 1208 if T1 == 16 & A1 == 1 
replace cmp_id = 35211  if T1 == 16 & A1 == 1 
// CDS-PP PPM
replace party_id = 1202 if T1 == 16 & A1 == 2 
replace cmp_id =  35520  if T1 == 16 & A1 == 2
// PAN
replace party_id = 1250 if T1 == 16 & A1 == 3 
replace cmp_id =  35120   if T1 == 16 & A1 == 3 
// PCP-PEV (I chose PCP as dominating for CMP)
replace party_id = 1201  if T1 == 16 & A1 == 4 
replace cmp_id =  35220   if T1 == 16 & A1 == 4  
// PPD/PSD
replace party_id = 1206 if T1 == 16 & A1 == 5
replace cmp_id = 35313   if T1 == 16 & A1 == 5 
// PPD/PSD-CDS-PP
replace party_id = . if T1 == 16 & A1 == 6
replace cmp_id =  35060  if T1 == 16 & A1 == 6
// PS
replace party_id = 1205 if T1 == 16 & A1 == 7
replace cmp_id = 35311 if T1 == 16 & A1 == 7


// Belgium 2014
// PS
replace party_id = 102 if T1 == 18 & A1 == 2
replace cmp_id = 21322  if T1 == 18 & A1 == 2
// MR
replace party_id = 106 if T1 == 18 & A1 == 5
replace cmp_id = 21426  if T1 == 18 & A1 == 5
// CDH
replace party_id = 108 if T1 == 18 & A1 == 4
replace cmp_id = 21522  if T1 == 18 & A1 == 4
// Ecolo
replace party_id = 104 if T1 == 18 & A1 == 3
replace cmp_id = 21111  if T1 == 18 & A1 == 3 
// FDF
replace party_id = 111 if T1 == 18 & A1 == 6 
replace cmp_id = 21423   if T1 == 18 & A1 == 6  
// VU
replace party_id = 110 if T1 == 18 & A1 == 11
replace cmp_id = 21913   if T1 == 18 & A1 == 11

// CDV
replace party_id = 109 if T1 == 18 & A1 == 14
replace cmp_id = 21521  if T1 == 18 & A1 == 14 
// NVA
replace party_id = 110 if T1 == 18 & A1 == 16
replace cmp_id = 21916  if T1 == 18 & A1 == 16 
// SPA
replace party_id = 103 if T1 == 18 & A1 == 12
replace cmp_id = 21321 if T1 == 18 & A1 == 12 
// Open VLD
replace party_id = 107 if T1 == 18 & A1 == 15 
replace cmp_id = 21421  if T1 == 18 & A1 == 15 
// VB
replace party_id = 112 if T1 == 18 & A1 == 18
replace cmp_id = 21917 if T1 == 18 & A1 == 18
// Groen!
replace party_id = 105 if T1 == 18 & A1 == 13
replace cmp_id = 21112  if T1 == 18 & A1 == 13 

// Ireland 2016
// FF
replace party_id = 701 if T1 == 23 & A1 == 2
replace cmp_id = 53620  if T1 == 23 & A1 == 2 
// FG
replace party_id = 702 if T1 == 23 & A1 == 1
replace cmp_id = 53520  if T1 == 23 & A1 == 1 
// Greens
replace party_id = 705 if T1 == 23 & A1 == 4
replace cmp_id = 53110 if T1 == 23 & A1 == 4
// Labour
replace party_id = 703 if T1 == 23 & A1 == 3
replace cmp_id = 53320  if T1 == 23 & A1 == 3 
// SF
replace party_id = 707 if T1 == 23 & A1 == 5
replace cmp_id = 53951  if T1 == 23 & A1 == 5


// Spain 2016
// Cs
// replace party_id =  if T1 == 26 & A1 == 1 
// replace cmp_id =  if T1 == 26 & A1 == 1 
// PP
replace party_id = 502 if T1 == 26 & A1 == 2 
replace cmp_id = 33610  if T1 == 26 & A1 == 2 
// PSOE
replace party_id = 501 if T1 == 26 & A1 == 3 
replace cmp_id = 33320 if T1 == 26 & A1 == 3
// UP/Podemos
// replace party_id =  if T1 == 26 & A1 == 4
// replace cmp_id =  if T1 == 26 & A1 == 4 


// UK 2015 + 2017
// Labor
replace party_id = 1102 if T1 == 29 & A1 == 3
replace cmp_id = 51320  if T1 == 29 & A1 == 3 
// Cons
replace party_id = 1101 if T1 == 29 & A1 == 1
replace cmp_id = 51620  if T1 == 29 & A1 == 1 
// LibDems
replace party_id = 1104 if T1 == 29 & A1 == 4
replace cmp_id = 51421  if T1 == 29 & A1 == 4 
// UKIP
replace party_id = 1108 if T1 == 29 & A1 == 8
replace cmp_id = 51951  if T1 == 29 & A1 == 8 
// GPEW
replace party_id = 1107 if T1 == 29 & A1 == 2
replace cmp_id = 51110 if T1 == 29 & A1 == 2
// SNP
replace party_id = 1105 if T1 == 29 & A1 == 7
replace cmp_id = 51902  if T1 == 29 & A1 == 7
// Plaid
replace party_id = 1106 if T1 == 29 & A1 == 5
replace cmp_id = 51901  if T1 == 29 & A1 == 5

// Labor
replace party_id = 1102 if T1 == 30 & A1 == 3
replace cmp_id = 51320  if T1 == 30 & A1 == 3 
// Cons
replace party_id = 1101 if T1 == 30 & A1 == 1
replace cmp_id = 51620  if T1 == 30 & A1 == 1 
// LibDems
replace party_id = 1104 if T1 == 30 & A1 == 4
replace cmp_id = 51421  if T1 == 30 & A1 == 4 
// UKIP
replace party_id = 1108 if T1 == 30 & A1 == 8
replace cmp_id = 51951  if T1 == 30 & A1 == 8 
// GPEW
replace party_id = 1107 if T1 == 30 & A1 == 2
replace cmp_id = 51110 if T1 == 30 & A1 == 2
// SNP
replace party_id = 1105 if T1 == 30 & A1 == 7
replace cmp_id = 51902  if T1 == 30 & A1 == 7
// Plaid
replace party_id = 1106 if T1 == 30 & A1 == 5
replace cmp_id = 51901  if T1 == 30 & A1 == 5


// Hungary 2014
// Fidesz
replace party_id = 2302 if T1 == 8 & A1 == 1
replace cmp_id = 86421 if T1 == 8 & A1 == 1
// KDNP
replace party_id = 2307 if T1 == 8 & A1 == 2
replace cmp_id = 86522  if T1 == 8 & A1 == 2
// MSZP
replace party_id = 2301 if T1 == 8 & A1 == 3
replace cmp_id = 86220  if T1 == 8 & A1 == 3
// Jobbik
replace party_id = 2308 if T1 == 8 & A1 == 4
replace cmp_id = 86710 if T1 == 8 & A1 == 4
// LMP
replace party_id = 2309 if T1 == 8 & A1 == 5
replace cmp_id = 86110 if T1 == 8 & A1 == 5
// Együtt
replace party_id = 2310 if T1 == 8 & A1 == 6
replace cmp_id = 86340  if T1 == 8 & A1 == 6
// Demokratikus koalíció
replace party_id = 2311 if T1 == 8 & A1 == 7
replace cmp_id = 86221  if T1 == 8 & A1 == 7
// Párbeszéd Magyarországért
replace party_id = . if T1 == 8 & A1 == 8
replace cmp_id = .  if T1 == 8 & A1 == 8


// Romania 2016
// PSD
replace party_id = 2701 if T1 == 13 & A1 == 1
replace cmp_id = 93223  if T1 == 13 & A1 == 1
// PNL
replace party_id = 2705 if T1 == 13 & A1 == 2
replace cmp_id = 93430  if T1 == 13 & A1 == 2
// USR
replace party_id = 2713 if T1 == 13 & A1 == 3
replace cmp_id = 93440  if T1 == 13 & A1 == 3
// UDMR
replace party_id = 2706 if T1 == 13 & A1 == 4
replace cmp_id = 93951  if T1 == 13 & A1 == 4
// ALDE
replace party_id = 2712 if T1 == 13 & A1 == 5
replace cmp_id = 93420  if T1 == 13 & A1 == 5
// PMP
replace party_id = 2711 if T1 == 13 & A1 == 6
replace cmp_id = 93540  if T1 == 13 & A1 == 6
// PCM/MPP
replace party_id = . if T1 == 13 & A1 == 7
replace cmp_id = .  if T1 == 13 & A1 == 7

// Estonia 2015
// Rahva Ühtsuse Erakond
replace party_id = . if T1 == 14 & A1 == 1
replace cmp_id = .  if T1 == 14 & A1 == 1
// Eesti Konservatiivne Rahvaerakond
replace party_id = 2209 if T1 == 14 & A1 == 2
replace cmp_id = 83720  if T1 == 14 & A1 == 2
// Eesti Reformierakond
replace party_id = 2203 if T1 == 14 & A1 == 3
replace cmp_id = 83430 if T1 == 14 & A1 == 3
// Erakond Isamaa ja Res Publica Liit
replace party_id = 2201 if T1 == 14 & A1 == 4
replace cmp_id = 83611  if T1 == 14 & A1 == 4
// Erakond Eestimaa Rohelised
replace party_id = 2207 if T1 == 14 & A1 == 5
replace cmp_id = 83110 if T1 == 14 & A1 == 5
// Eesti Ühendatud Vasakpartei
replace party_id = 2205 if T1 == 14 & A1 == 6
replace cmp_id = 83952 if T1 == 14 & A1 == 6
// Eesti Vabaerakond
replace party_id = 2208 if T1 == 14 & A1 == 7
replace cmp_id = 83440  if T1 == 14 & A1 == 7
// Eesti Keskerakond
replace party_id = 2202 if T1 == 14 & A1 == 8
replace cmp_id = 83411 if T1 == 14 & A1 == 8
// Sotsiaaldemokraatlik Erakond
replace party_id = 2204 if T1 == 14 & A1 == 9
replace cmp_id = 83410 if T1 == 14 & A1 == 9
// Eesti Iseseisvuspartei
replace party_id = . if T1 == 14 & A1 == 10
replace cmp_id = .  if T1 == 14 & A1 == 10
// Üksikkandidaat
replace party_id = . if T1 == 14 & A1 == 11
replace cmp_id = .  if T1 == 14 & A1 == 11

// Estonia 2019
// Reformierakond
replace party_id = 2203 if T1 == 27 & A1 == 2
replace cmp_id = 83430 if T1 == 27 & A1 == 2
// SDE
replace party_id = 2204 if T1 == 27 & A1 == 3
replace cmp_id = 83410 if T1 == 27 & A1 == 3
// EKRE
replace party_id = 2209 if T1 == 27 & A1 == 4
replace cmp_id = 83720  if T1 == 27 & A1 == 4
// Keskerakond
replace party_id = 2202 if T1 == 27 & A1 == 5
replace cmp_id = 83411 if T1 == 27 & A1 == 5
// Isamaa
replace party_id = 2201 if T1 == 27 & A1 == 6
replace cmp_id = 83611 if T1 == 27 & A1 == 6



// Czech Republic 2017
// ANO 2011
replace party_id = 2111 if T1 == 21 & A1 == 1
replace cmp_id = 82430  if T1 == 21 & A1 == 1
// ČSSD
replace party_id = 2101 if T1 == 21 & A1 == 2
replace cmp_id = 82320  if T1 == 21 & A1 == 2
// KDU-ČSL
replace party_id = 2104 if T1 == 21 & A1 == 3
replace cmp_id = 82523 if T1 == 21 & A1 == 3
// KSČM
replace party_id = 2103 if T1 == 21 & A1 == 4
replace cmp_id = 82220 if T1 == 21 & A1 == 4
// ODS
replace party_id = 2102 if T1 == 21 & A1 == 5
replace cmp_id = 82413  if T1 == 21 & A1 == 5
// Piráti
replace party_id = 2114 if T1 == 21 & A1 == 6
replace cmp_id = 82953 if T1 == 21 & A1 == 6
// SPD
replace party_id = 2115 if T1 == 21 & A1 == 7
replace cmp_id = 82721 if T1 == 21 & A1 == 7
// STAN
replace party_id = 2116 if T1 == 21 & A1 == 8
replace cmp_id = 82610  if T1 == 21 & A1 == 8
// TOP 09
replace party_id = 2109 if T1 == 21 & A1 == 9
replace cmp_id = 82421  if T1 == 21 & A1 == 9


//drop if cmp_id == . 
// drop CSU
//drop if party_id == 308


drop if party_id == .
// drop UK in 2015
drop if T1 == 29
// drop Estonia in 2015
drop if T1 == 14
// drop UK 2017
drop if T1 == 30 
// drop Portugal 2015
drop if T1 == 16
// drop Finland 2015
drop if T1 == 15


save "CCS_2_manipulated.dta", replace

// wave 3

use "CCS wave 3.dta", replace

order A1 T9
replace A1 = T9 if A1 < 0 & T9 <= 100

// rescale the variables to 0-10 scale
// immigrants should adjust to customs 1 = strongly disagree, 5 = strongly agree
gen multicult_divisions_ccs3 = C2a if C2a >= 0 & C2a <= 10
replace multicult_divisions_ccs3 = (multicult_divisions_ccs3-1)/(5-1)
replace multicult_divisions_ccs3 = multicult_divisions_ccs3*10

// 1 = immigrants bad for the economy, 5 = good
gen imm_eco_divisions_ccs3 = C2i if C2i >= 0 & C2i <= 10
replace imm_eco_divisions_ccs3 = (imm_eco_divisions_ccs3-1)/(5-1)
replace imm_eco_divisions_ccs3 = imm_eco_divisions_ccs3*10 

gen environment_divisions_ccs3 = C2c if C2c >= 0 & C2c <= 10
replace environment_divisions_ccs3 = (environment_divisions_ccs3-1)/(5-1)
replace environment_divisions_ccs3 = environment_divisions_ccs3*10

gen eu_divisions_ccs3 = C7 if C7 >= 0 & C7 <= 10


gen lr_divisions_ccs3 = C5a if C5a >= 0 & C5a <= 10


// state intervention in the economy is bad, 1 = stronly agree
// transform to left-right scale
gen ecoint_divisions_ccs3 = .
replace ecoint_divisions_ccs3 = 6-C2b if C2b >= 0 & C2b <= 10
replace ecoint_divisions_ccs3 = (ecoint_divisions_ccs3-1)/(5-1)
replace ecoint_divisions_ccs3 = ecoint_divisions_ccs3*10

// redistribution, 1 = strongly agree
gen redist_divisions_ccs3 = C2h if C2h >= 0 & C2h <= 10
replace redist_divisions_ccs3 = (redist_divisions_ccs3-1)/(5-1)
replace redist_divisions_ccs3 = redist_divisions_ccs3*10

// mix the two economy indicators
// 0 = left, 10 = right
gen economy_divisions_ccs3 = ecoint_divisions_ccs3 + redist_divisions_ccs3 if ecoint_divisions_ccs3 >= 0 & ecoint_divisions_ccs3 <= 10 & redist_divisions_ccs3 >= 0 & redist_divisions_ccs3 <= 10
replace economy_divisions_ccs3 = (economy_divisions_ccs3-0)/(20-0)
replace economy_divisions_ccs3 = economy_divisions_ccs3*10

// question about same-sex marriage, 1 = strongly agree prohibition
gen ssm_divisions_ccs3 = .
replace ssm_divisions_ccs3 = 6-C2d if C2d >= 0 & C2d <= 10
replace ssm_divisions_ccs3 = (ssm_divisions_ccs3-1)/(5-1)
replace ssm_divisions_ccs3 = ssm_divisions_ccs3*10

// abortion question, 1 = free to decide
gen abortion_divisions_ccs3 = C2j if C2j >= 0 & C2j <= 10

replace abortion_divisions_ccs3 = (abortion_divisions_ccs3-1)/(5-1)
replace abortion_divisions_ccs3 = abortion_divisions_ccs3*10

// stiffer sentences for law breakers, 1 =  strongly agree
gen order_divisions_ccs3 = .
replace order_divisions_ccs3 = 6-C2f if C2f >= 0 & C2f <= 10

replace order_divisions_ccs3 = (order_divisions_ccs3-1)/(5-1)
replace order_divisions_ccs3 = order_divisions_ccs3*10

// mix the two GALTAN indicators
// 0 = GAL, 10 = TAN
gen galtan_divisions_ccs3 = ssm_divisions_ccs3 + abortion_divisions_ccs3 + order_divisions_ccs3 if ssm_divisions_ccs3 != . & abortion_divisions_ccs3 != . & order_divisions_ccs3 != .
replace galtan_divisions_ccs3 = (galtan_divisions_ccs3-0)/(30-0)
replace galtan_divisions_ccs3 = galtan_divisions_ccs3*10

// unify CDU Portugal 
replace A1 = 4 if A1 == 5 & T1 == "PRT_2019" 

// drop all parties with less than 20 observations
gen counter = 1
bysort T1 T3 A1: egen party_observations = count(counter)
keep if party_observations >= 10


collapse (sd) multicult_divisions_ccs3 imm_eco_divisions_ccs3 environment_divisions_ccs3 eu_divisions_ccs3 economy_divisions_ccs3 lr_divisions_ccs3 ssm_divisions_ccs3 abortion_divisions_ccs3 order_divisions_ccs3 galtan_divisions_ccs3 ecoint_divisions_ccs3 redist_divisions_ccs3, by(T1 T3 A1)

save "CCS3_manipulated.dta", replace

use "CCS3_manipulated.dta", replace

gen year = T3 

gen ches_year = .
replace ches_year = 2019 if year == 2018
replace ches_year = 2019 if year == 2019
replace ches_year = 2024 if year > 2019

// need to add party ids to the variables
gen party_id = .
gen cmp_id = .
// Germany 2021 / be careful when matching with CDU/CSU, generate an average score?
// SPD
replace party_id = 302 if T1 == "GER_2021" & A1 == 4 
replace cmp_id = 41320  if T1 == "GER_2021" & A1 == 4 
// CDU
replace party_id = 301 if T1 == "GER_2021" & A1 == 2
replace cmp_id = 41521 if T1 == "GER_2021" & A1 == 2 
// CSU
replace party_id = 308 if T1 == "GER_2021" & A1 == 3 
replace cmp_id = 41521  if T1 == "GER_2021" & A1 == 3
// FDP
replace party_id = 303 if T1 == "GER_2021" & A1 == 6 & T3 == 2021
replace cmp_id = 41420  if T1 == "GER_2021" & A1 == 6 & T3 == 2021
// Gruene
replace party_id = 304 if T1 == "GER_2021" & A1 == 8 & T3 == 2021
replace cmp_id = 41113  if T1 == "GER_2021" & A1 == 8 & T3 == 2021
// Left 
replace party_id = 306 if T1 == "GER_2021" & A1 == 7 & T3 == 2021
replace cmp_id = 41223 if T1 == "GER_2021" & A1 == 7 & T3 == 2021
// AfD
replace party_id = 310 if T1 == "GER_2021" & A1 == 5 & T3 == 2021
replace cmp_id = 41953  if T1 == "GER_2021" & A1 == 5 & T3 == 2021

// Sweden 2018
// C
replace party_id = 1603 if T1 == "SWE_2018" & A1 == 3
replace cmp_id = 11810  if T1 == "SWE_2018" & A1 == 3 
// FI
replace party_id = 1612 if T1 == "SWE_2018" & A1 == 9
replace cmp_id = . if T1 == "SWE_2018" & A1 == 9
// L
replace party_id = 1604 if T1 == "SWE_2018" & A1 == 4
replace cmp_id = 11420  if T1 == "SWE_2018" & A1 == 4
// KD
replace party_id = 1606 if T1 == "SWE_2018" & A1 == 6
replace cmp_id = 11520  if T1 == "SWE_2018" & A1 == 6 
// MP
replace party_id = 1607 if T1 == "SWE_2018" & A1 == 7
replace cmp_id = 11110 if T1 == "SWE_2018" & A1 == 7
// M
replace party_id = 1605 if T1 == "SWE_2018" & A1 == 5
replace cmp_id = 11620  if T1 == "SWE_2018" & A1 == 5
// S
replace party_id = 1602  if T1 == "SWE_2018" & A1 == 2
replace cmp_id = 11320  if T1 == "SWE_2018" & A1 == 2
// SD
replace party_id = 1610 if T1 == "SWE_2018" & A1 == 8
replace cmp_id = 11710   if T1 == "SWE_2018" & A1 == 8
// V
replace party_id = 1601 if T1 == "SWE_2018" & A1 == 1
replace cmp_id = 11220 if T1 == "SWE_2018" & A1 == 1

// Finland 2019

// Centre
replace party_id = 1403 if T1 == "FIN_2019" & A1 == 4
replace cmp_id = 14810  if T1 == "FIN_2019" & A1 == 4
// National Coalition
replace party_id = 1402 if T1 == "FIN_2019" & A1 == 3 
replace cmp_id = 14620  if T1 == "FIN_2019" & A1 == 3
// Social Dems
replace party_id = 1401 if T1 == "FIN_2019" & A1 == 1
replace cmp_id = 14320  if T1 == "FIN_2019" & A1 == 1 
// Left Alliance
replace party_id = 1404 if T1 == "FIN_2019" & A1 == 6
replace cmp_id = 14223  if T1 == "FIN_2019" & A1 == 6  
// Greens
replace party_id = 1408 if T1 == "FIN_2019" & A1 == 5
replace cmp_id = 14110 if T1 == "FIN_2019" & A1 == 5
// KD
replace party_id = 1409 if T1 == "FIN_2019" & A1 == 8
replace cmp_id = 14520 if T1 == "FIN_2019" & A1 == 8
// Swedish
replace party_id = 1406 if T1 == "FIN_2019" & A1 == 7
replace cmp_id = 14901  if T1 == "FIN_2019" & A1 == 7 
// PS
replace party_id = 1405 if T1 == "FIN_2019" & A1 == 2
replace cmp_id = 14820  if T1 == "FIN_2019" & A1 == 2 
// Liike Nyt
replace party_id = 1410 if T1 == "FIN_2019" & A1 == 10
replace cmp_id = 14440  if T1 == "FIN_2019" & A1 == 10

// Portugal 2019
// BE
replace party_id = 1208 if T1 == "PRT_2019" & A1 == 1 
replace cmp_id = 35211  if T1 == "PRT_2019" & A1 == 1 
// CDS-PP PPM
replace party_id = 1202 if T1 == "PRT_2019" & A1 == 2 
replace cmp_id =  35520  if T1 == "PRT_2019" & A1 == 2
// PAN
replace party_id = 1250 if T1 == "PRT_2019" & A1 == 3 
replace cmp_id =  35120   if T1 == "PRT_2019" & A1 == 3 
// CDU (both components)
replace party_id = 1201  if T1 == "PRT_2019" & A1 == 4 
replace cmp_id =  35220   if T1 == "PRT_2019" & A1 == 4  
replace party_id = 1201  if T1 == "PRT_2019" & A1 == 5 
replace cmp_id =  35220   if T1 == "PRT_2019" & A1 == 5  
// PPD/PSD
replace party_id = 1206 if T1 == "PRT_2019" & A1 == 6
replace cmp_id = 35313   if T1 == "PRT_2019" & A1 == 6
// PS
replace party_id = 1205 if T1 == "PRT_2019" & A1 == 7
replace cmp_id = 35311 if T1 == "PRT_2019" & A1 == 7
// IL
replace party_id = 1252 if T1 == "PRT_2019" & A1 == 9
replace cmp_id = 35410 if T1 == "PRT_2019" & A1 == 9


// UK 2019
// Labor
replace party_id = 1102 if T1 == "GBR_2019" & A1 == 3
replace cmp_id = 51320  if T1 == "GBR_2019" & A1 == 3 
// Cons
replace party_id = 1101 if T1 == "GBR_2019" & A1 == 1
replace cmp_id = 51620  if T1 == "GBR_2019" & A1 == 1 
// LibDems
replace party_id = 1104 if T1 == "GBR_2019" & A1 == 4
replace cmp_id = 51421  if T1 == "GBR_2019" & A1 == 4 
// GPEW
replace party_id = 1107 if T1 == "GBR_2019" & A1 == 2
replace cmp_id = 51110 if T1 == "GBR_2019" & A1 == 2
// SNP
replace party_id = 1105 if T1 == "GBR_2019" & A1 == 6
replace cmp_id = 51902  if T1 == "GBR_2019" & A1 == 6
// Plaid
replace party_id = 1106 if T1 == "GBR_2019" & A1 == 5
replace cmp_id = 51901  if T1 == "GBR_2019" & A1 == 5
// BREXIT
replace party_id = 1110 if T1 == "GBR_2019" & A1 == 7
replace cmp_id = .  if T1 == "GBR_2019" & A1 == 7


// Romania 2020
// PSD
replace party_id = 2701 if T1 == "ROU_2020" & A1 == 3
replace cmp_id = 93223  if T1 == "ROU_2020" & A1 == 3
// PNL
replace party_id = 2705 if T1 == "ROU_2020" & A1 == 2
replace cmp_id = 93430 if T1 == "ROU_2020" & A1 == 2
// USR
replace party_id = 2713 if T1 == "ROU_2020" & A1 == 4
replace cmp_id = 93440  if T1 == "ROU_2020" & A1 == 4
// UDMR
replace party_id = 2706 if T1 == "ROU_2020" & A1 == 5
replace cmp_id = 93951  if T1 == "ROU_2020" & A1 == 5
// AUR
replace party_id = 2715 if T1 == "ROU_2020" & A1 == 1
replace cmp_id = .  if T1 == "ROU_2020" & A1 == 1


// Ireland 2020
// FF
replace party_id = 701 if T1 == "IRL_2020" & A1 == 2
replace cmp_id = 53620  if T1 == "IRL_2020" & A1 == 2 
// FG
replace party_id = 702 if T1 == "IRL_2020" & A1 == 1
replace cmp_id = 53520  if T1 == "IRL_2020" & A1 == 1 
// Greens
replace party_id = 705 if T1 == "IRL_2020" & A1 == 4
replace cmp_id = 53110 if T1 == "IRL_2020" & A1 == 4
// Labour
replace party_id = 703 if T1 == "IRL_2020" & A1 == 3
replace cmp_id = 53320  if T1 == "IRL_2020" & A1 == 3 
// SF
replace party_id = 707 if T1 == "IRL_2020" & A1 == 5
replace cmp_id = 53951  if T1 == "IRL_2020" & A1 == 5
// SD
replace party_id = 710 if T1 == "IRL_2020" & A1 == 6
replace cmp_id = 53321 if T1 == "IRL_2020" & A1 == 6
// PBP
replace party_id = 709 if T1 == "IRL_2020" & A1 == 7
replace cmp_id = 53231 if T1 == "IRL_2020" & A1 == 7
// Renua Ireland
replace party_id = 712 if T1 == "IRL_2020" & A1 == 9
replace cmp_id = . if T1 == "IRL_2020" & A1 == 9
// Aontu
replace party_id = 713 if T1 == "IRL_2020" & A1 == 10
replace cmp_id = 53952 if T1 == "IRL_2020" & A1 == 10
//drop if cmp_id == . 
// drop CSU
//drop if party_id == 308


drop if party_id == .


save "CCS_3_manipulated.dta", replace



















